<script>
import QSGDrawer from "@/components/QSGDrawer/index.vue";
import {addOrUpdateBackground} from "@/api/aihuman/humanAssets/backgroundGallery";
import {uploadFile} from "@/api/aihuman/common/minio";
import {addOrUpdateVideo} from "@/api/aihuman/humanAssets/videoGallery";

export default {
  name: "AddVideo",
  components: {QSGDrawer},
  data() {
    //检验声音id
    var checkType = (rule, value, callback) => {
      // var inputType = /[^\w]/g
      var inputReg = /[a-zA-Z0-9-_]/g;
      if (!inputReg.test(value)) {
        callback(new Error('id由数字、英文、短划线、下划线组成'))
      } else {
        callback()
      }
    };
    return {
      formStatus: 'add',
      showDrawer: false,
      title: "新增",
      loading: false,
      groupId: '',
      form: {
        name: '',
        id: '',
        previewUrl: '',
        orientation: '',
        status: '',
      },
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      rules: {
        name: [
          {
            required: true,
            message: '请输入图像名称',
            trigger: 'blur'
          },
          {min: 1, max: 30, message: '长度不可超过30个字符', trigger: 'blur'}
        ],
        id: [
          {required: true, message: '请输入图像id', trigger: 'blur'},
          {min: 1, max: 50, message: '长度不可超过50个字符', trigger: 'blur'},
          {required: true, validator: checkType, trigger: 'blur'}
        ],
        galleryVideoLink: [
          {required: true, message: '请上传图像', trigger: 'change'},
        ],
        backgroundType: [
          {required: true, message: '请输入选择类型', trigger: 'change'},
        ],
      }
    }
  },
  methods: {
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    open(type, data = {}) {
      console.log(data);
      if (type === 'add') {
        this.showDrawer = true
        this.formStatus = type
        this.form = {
          name: '',
          id: '',
          previewUrl: '',
          orientation: '',
          status: '',
        }
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      } else if (type === 'preview') {
        this.title = '查看'
        this.formStatus = type
        this.showDrawer = true
        this.form = {...data}
      } else {
        this.title = '编辑'
        this.formStatus = type
        this.showDrawer = true
        this.form = {...data}
      }
    },
    async uploadGallery(file) {
      let formData = new FormData()
      formData.append('file', file.file)
      formData.append('keyFolderPath', `HumanAssets/videoGallery`)

      const {data} = await uploadFile(formData)
      this.form.previewUrl = data.data.filePath
      this.$refs.form.validateField('previewUrl')
    },
    closeDawer() {
      this.showDrawer = false
    },
    submitData() {
      this.loading = true
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          let params = {
            ...this.form
          }

          try {
            const {data} = await addOrUpdateVideo(params)
            console.log(data);
            this.loading = false
            this.showDrawer = false
            this.$emit('save')
          } catch (e) {
            console.log(e);
            this.loading = false
          }

        } else {
          console.log('error submit!!');
          this.loading = false
          return false;
        }
      });
    }
  }
}
</script>

<template>
  <q-s-g-drawer
    ref="drawer"
    :visible.sync="showDrawer"
    size="600px"
    :title="title"
  >

    <el-form ref="form" :model="form" :rules="rules" label-width="90px" size="small">
      <el-form-item label="视频名称:" prop="name">
        <el-input v-if="formStatus !== 'preview'" v-model="form.name"></el-input>
        <div v-else>{{ form.name }}</div>
      </el-form-item>

      <el-form-item label="视频ID:" prop="id">
        <el-input v-if="formStatus === 'add'" v-model="form.id"></el-input>
        <div v-else>{{ form.id }}</div>
      </el-form-item>

      <el-form-item label="尺寸:" prop="orientation">
        <el-radio-group v-if="formStatus !== 'preview'" v-model="form.orientation">
          <el-radio :label="1">横屏16:9</el-radio>
          <el-radio :label="2">竖屏9:16</el-radio>
        </el-radio-group>
        <div v-else>{{ form.$orientation }}</div>
      </el-form-item>

      <el-form-item label="启用状态:">
        <el-switch
          v-model="form.status"
          active-text="停用"
          inactive-text="启用"
          :active-value="1"
          :disabled="formStatus === 'preview'"
          :inactive-value="0"
        >
        </el-switch>
      </el-form-item>

<!--      <el-form-item label="标签:">
        <template v-if="formStatus === 'preview'">
          <el-tag
            :key="tag"
            v-for="tag in dynamicTags"
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{ tag }}
          </el-tag>
        </template>
        <template v-else>
          <el-tag
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{ tag }}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
        </template>
      </el-form-item>-->

      <el-form-item label="视频预览:" prop="previewUrl">
        <el-upload
          v-if="formStatus !== 'preview'"
          class="upload-demo"
          :http-request="uploadGallery"
          accept=".mp4"
          :show-file-list="false"
          drag>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传mp4文件</div>
        </el-upload>

        <div v-if="form.previewUrl">
          <video :src="`${window.location.origin}` + form.previewUrl" alt="" style="height: 100px" controls></video>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button
        v-if="formStatus !== 'preview'"
        type="primary"
        icon="el-icon-circle-plus-outline"
        size="small"
        :loading="loading"
        @click="submitData"
      >保 存
      </el-button>
      <el-button
        type="default"
        icon="el-icon-circle-close"
        size="small"
        :loading="loading"
        @click="closeDawer"
      >{{ formStatus === 'preview' ? '关 闭' : '取 消' }}
      </el-button>
    </template>
  </q-s-g-drawer>
</template>

<style scoped lang="scss">
.el-tag + .el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
